﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;

public partial class ClassManager_AddType : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            RECORDID.Text = "";
            String rID = Page.Request["ID"];
            if (rID != null && rID != "")
            {
                EntityOprHelper helper = new EntityOprHelper();
                T_Type add = helper.GetEntityByWhere<T_Type>(" where ID=" + rID + "");
                if (add != null)
                {
                    RECORDID.Text = add.ID.ToString();
                    pName.Text = add.NAME;
                    pURL.Text = add.URL;
                    pImage.Text = add.IMAGE;
                    pType.SelectedValue = add.SHOWMODE;

                }
            }

        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        pImage.Text=shareCode.upFileToServer();
    }
    //protected string upFileToServer()
    //{
    //    System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
    //    String path = ConfigurationSettings.AppSettings["ImagesPath"]; //+ "Images/BookImages/";  //设置服务器上传的路径，即文件上传的位置
    //    for (int i = 0; i < files.Count; i++)
    //    {
    //        bool bIs = false;
    //        HttpPostedFile postfiles = files[i];
    //        //Bitmap outputfile = null;
    //        string fileName = System.IO.Path.GetFileName(postfiles.FileName);
    //        String fileExtension = System.IO.Path.GetExtension(postfiles.FileName).ToLower();
    //        String[] allowedExtensions = { ".gif", ".jpg", ".png", ".jpeg" };
    //        for (int nCount = 0; nCount < allowedExtensions.Length; nCount++)
    //        {
    //            if (fileExtension == allowedExtensions[nCount])
    //            {
    //                bIs = true;
    //            }
    //        }
    //        if (bIs)
    //        {
    //            try
    //            {
    //                string strName = path + fileName;
    //                postfiles.SaveAs(strName);
    //                return fileName;
    //            }
    //            catch (Exception exp)
    //            {
    //                throw exp;
    //            }
    //        }
    //        else
    //        {
    //            //lblErrorMessage.Text = "不能接受这种文件类型。";
    //            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('不能接受这种文件类型!')", true);

    //        }
    //    }
    //    return "";
    //}


    protected void Submit_Click(object sender, EventArgs e)
    {
        EntityOprHelper helper = new EntityOprHelper();

        if (RECORDID.Text == "")
        {
            if (pName.Text == "")
            {

                return;
            }
            
            T_Type _tp = helper.GetEntityByWhere<T_Type>(" where NAME='" + pName.Text + "';");
            if (_tp != null)
            {

                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('该类型已经存在!')", true);
                return;
            }


            T_Type type = new T_Type();
            type.TYPEID = System.Guid.NewGuid().ToString("N").ToUpper();
            type.NAME = pName.Text;
            type.IMAGE = pImage.Text;
            type.URL = pURL.Text;
            type.SHOWMODE = pType.SelectedValue;
            bool b=helper.AddEntity<T_Type>(type);
            if (b)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加成功!')", true);
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('添加失败!')", true);
            }
        }
        else 
        {
            T_Type address = new T_Type();
            address.NAME = pName.Text;
            address.IMAGE = pImage.Text;
            address.URL = pURL.Text;
            address.SHOWMODE = pType.SelectedValue;
            address.ID =Convert.ToInt32(RECORDID.Text);

            bool b1 = helper.EditEntity<T_Type>(address);
            if (b1)
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改成功')", true);
                pName.Text = "";
                RECORDID.Text = "";
            }
            else
            {
                ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "", "alert('修改失败')", true);
            }

            return;
        
        }
    }
}
